{% extends '../_manage.html' %}

{% block title %} {{ form.name }} {% endblock %}

{% block head %}

<script>
$(function() {
    getJSON('/api/navigations/all/menus', function (err, data) {
        if (err) {
            return showError(err);
        }
        initVM(data.navigationMenus);
    });
});

function initVM(menus) {
    var vm = new Vue({
        el: '#vm',
        data: {
            menus: menus,
            selectUrl: '',
            name: '',
            url: '',
        },
        created: function () {
            $('#loading').hide();
            $('#vm').show();
        },
        methods: {
            selectMenu: function () {
                var that = this;
                var i, selected = that.selectUrl;
                for (i=0; i<menus.length; i++) {
                    if (menus[i].url === selected) {
                        that.name = menus[i].name;
                        that.url = menus[i].url;
                        break;
                    }
                }
            },
            submit: function () {
                var that = this;
                that.$resource('{{ form.action }}').save({
                    name: this.name,
                    url: this.url
                }).then(function (resp) {
                    resp.json().then(function (result) {
                        location.assign('{{ form.redirect }}');
                    });
                }, onJsonFormError);
            }
        }
    });
}
</script>

{% endblock %}

{% block main %}

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> {{ _('Loading') }}...
    </div>

    <div id="vm" class="uk-width-1-1">
        <ul class="uk-breadcrumb">
            <li><a href="navigation_list">{{ _('All Navigations') }}</a></li>
            <li class="uk-active"><span>{{ form.name }}</span></li>
        </ul>

        <form v-on:submit.prevent="submit" class="uk-form uk-form-stacked uk-margin">
            <legend>{{ form.name }}</legend>
            <fieldset>
                <div class="uk-alert uk-alert-danger uk-hidden"></div>

                <div class="uk-form-row">
                    <label class="uk-form-label">Type:</label>
                    <div class="uk-form-controls">
                        <select v-model="selectUrl" v-on:change="selectMenu">
                            <option value="">- select navigation -</option>
                            <option v-for="m in menus" v-bind:value="m.url" v-text="m.name"></option>
                        </select>
                    </div>
                </div>

                <div class="uk-form-row">
                    <label class="uk-form-label">Name:</label>
                    <div class="uk-form-controls">
                        <input v-model="name" name="name" type="text" maxlength="100" class="uk-width-1-1" placeholder="name">
                    </div>
                </div>

                <div class="uk-form-row">
                    <label class="uk-form-label">URL:</label>
                    <div class="uk-form-controls">
                        <input v-model="url" name="url" type="text" maxlength="100" class="uk-width-1-1" placeholder="http://">
                    </div>
                </div>

                <div class="uk-form-row">
                    <div class="uk-form-controls">
                        <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-check"></i> Save</button>
                        <button type="button" class="uk-button" onclick="location.assign('{{ form.redirect }}')"><i class="uk-icon-times"></i> Cancel</button>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>

{% endblock %}
